package com.jianyun.jyzs.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import com.google.gson.reflect.TypeToken;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.EngDetialActivity;
import com.jianyun.jyzs.bean.EdMessageBean;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.bean.MessageData;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.dao.EngineerDao;
import com.jianyun.jyzs.utils.DateTime;
import com.jianyun.jyzs.utils.LoginCache;
import com.squareup.picasso.Picasso;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.rongcloud.rce.lib.SimpleResultCallback;
import cn.rongcloud.rce.lib.UserTask;
import cn.rongcloud.rce.lib.model.StaffInfo;

/**
 * Created by mlli on 2017/11/15.
 */

public class EngMessageAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<MessageData> list;
    private final EngineerDao dao;
    private final String enterpriseCode;
    //private final String bcfHttpUrl;
    private final Gson gson;
    private final String erpRootUrl;

    public EngMessageAdapter(Context context, List<MessageData> list) {
        this.context = context;
        this.list = list;
        dao = new EngineerDao(context);
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        //bcfHttpUrl = LoginCache.getInstance().getLoginCache().getBcfHttpUrl();
        gson = new Gson();
        erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl() + "DataService/browseImage.aspx?autoid=";
    }

    public void setList(List<MessageData> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_ed_message, parent, false);
        return new MHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final MHolder mHolder = (MHolder) holder;
        final MessageData bean = list.get(position);
        if (bean != null) {
            mHolder.t3.setText(bean.getDt());
            String type1 = bean.getTYPE1();
            mHolder.t1.setText(bean.getFromUserName());
            switch (type1) {
                case "2":
                    Picasso.with(context).load(R.drawable.audio_paly_002).into(mHolder.image);
                    break;
                case "1":
                    String voiceUrl = erpRootUrl + bean.getId();
                    Picasso.with(context).load(voiceUrl).into(mHolder.image);
                    break;
                case "3":
                    String voiceUrla = erpRootUrl + bean.getId();
                    Glide.with(context).load(voiceUrla).into(mHolder.head);
                    //Picasso.with(context).load(R.drawable.video_record).into(mHolder.image);
                    break;
            }
        }

        Picasso.with(context).load(R.drawable.default_fmessage).into(mHolder.head);

        String type = bean.getType();
        switch (type) {
            case "Agree"://点赞
                mHolder.t2.setText("  点赞  ");
                break;
            case "Discuss"://评论
                mHolder.t2.setText("评论：" + bean.getTextStr());
                break;

        }
        mHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /**
                 * TODO 待完成
                 */
                context.startActivity(new Intent(context,EngDetialActivity.class).putExtra(SysConstant.msg_key_string1,bean.getProjectProgress()));
            }
        });
    }

    private int attachType(String phonPath, String photoPath, String videoPath) {
        int type = -1;
        if (!TextUtils.isEmpty(phonPath)) type = 0;
        if (!TextUtils.isEmpty(photoPath)) type = 1;
        if (!TextUtils.isEmpty(videoPath)) type = 2;
        return type;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.head)
        ImageView head;
        @BindView(R.id.t1)
        TextView t1;
        @BindView(R.id.t2)
        TextView t2;
        @BindView(R.id.t3)
        TextView t3;
        @BindView(R.id.image)
        ImageView image;

        public MHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
